<%--                                                                       --%>
<%-- Copyright (c) 2005 BroadVision, Inc.  All rights reserved.            --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>

<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%
  String ctxPath = request.getContextPath();
%>

<script language="JavaScript">

// Save the modified DIV mapping(s) or the new DIV mapping
function doDivMappingSave(form) {
  form.action = "<c:url value="/ep/channelDivMappingsMultiSave.do"/>";
  return true;
}

var divMappingName;
var divMappingProgram;

// Put a program to the program field from candidates
function replaceProgram(divName) {
  var src = "candidatePrograms" + divName;
  divMappingName = divName;
  var dest = "programName" + divName;

  var srcElement = document.epChannelDivMappingsEditForm[src];
  var destElement = document.epChannelDivMappingsEditForm[dest];

  var len = srcElement.length;

  for (var index = 0; index < len; index++) {
    if (srcElement.options[index].selected) {
	  if (srcElement.options[index].text == "<bean-el:message key="ep.channel.div.mappings.select.program" bundle="ep.messages"/>" ||
		  srcElement.options[index].text == "---------------" ) {
		  break;
	  }
	  else if (srcElement.options[index].text == "<bean-el:message key="ep.channel.div.mappings.more.selection" bundle="ep.messages"/>")
	  {
		doPickerPopupAction('<c:url value="/mt/pickerPopupSearchSingleSelect.do"/>', 'setPrograms', 301, null, null);
	  }
	  else {
		destElement.value = srcElement.options[index].text;
		var programText = document.getElementById("programNameText" + divName);
		programText.innerHTML = srcElement.options[index].text;
	  }
	  divMappingProgram = srcElement.options[index].text;
	  break;
    }
  }

  srcElement.options[0].selected = true;

}

// Call back program to set program field from picker.
function setPrograms(ids, names) {
  var dest = "programName" + divMappingName;
  var destElement = document.epChannelDivMappingsEditForm[dest];
  destElement.value = names[0];		
  var programText = document.getElementById("programNameText" + divMappingName);
  programText.innerHTML = names[0];


  var src = "candidatePrograms" + divMappingName;
  var srcElement = document.epChannelDivMappingsEditForm[src];
  if (divMappingProgram == "<bean-el:message key="ep.channel.div.mappings.more.selection" bundle="ep.messages"/>")
  {
	srcElement.options[0].selected = true;
  }
}

// Cancel the Edit action and go back to DIV Mappings View
function doCancel(form) {
  form.action = "<c:url value="/ep/channelDivMappingsView.do"/>";
  form.submit();
}

function trimTextFields(myform) {
  var theDivName = myform.divName.value;
  myform.divName.value = theDivName.stringTrim();
  var theProgramName = myform.programName.value;
  myform.programName.value = theProgramName.stringTrim();
}

// Before save, do some validation work
function validateForm() {

	form = document.epChannelDivMappingsEditForm;
	if (form.createNew.value == "1")
	{
	    trimTextFields(form);
		if (form.programName.value == null || form.programName.value == "" || 
			form.divName.value == null || form.divName.value == "")
	    {
			alert("<bean-el:message key="ep.channel.div.mappings.error.no.empty" bundle="ep.messages"/>");
			return false;
	    }
	}
	else {
<c:forEach var="item" items="${epChannelDivMappingsEdit.map.divMappings}" varStatus="index">
	var theProgramName = 
	  form.programName<c:out value="${item.map.divName}"/>.value;
	form.programName<c:out value="${item.map.divName}"/>.value =
	  theProgramName.stringTrim();
	if (form.programName<c:out value="${item.map.divName}"/>.value == null ||
		form.programName<c:out value="${item.map.divName}"/>.value == "" ||
		form.divNewName.value == null ||
		form.divNewName.value.stringTrim() == "")
	{
		alert("<bean-el:message key="ep.channel.div.mappings.error.no.empty" bundle="ep.messages"/>");
		return false;
	}
</c:forEach>
	}
	return true;
}

<%@ include file="/mc/common/jsutils.jsp" %>

</script>

<%@include file="/mt/common/pickerPopupHelper.jsp" %>

<!-- Prepare page title -->
<c:set var="pageTitle">
<bean-el:message key="ep.channel.view.page.title" bundle="ep.messages" 
  arg0="${epChannelDivMappingsEdit.map.name}"/>
</c:set>
<c:set var="editable" value="${epChannelDivMappingsEdit.map.editable}"/>

<!-- Display main body -->
<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr>
    <td valign="top" width="100%">

<!-- Page title and tabs -->
      <div class="pageTitle">
        <c:out value="${pageTitle}"/>
      </div> 
      <div class="errorMessage"><html-el:errors bundle="ep.messages"/></div>
      <%@include file="/mc/common/pagetab.jsp" %>

<!-- Main body of DIV Mapping page -->      
      <div class="breadcrumb">
      <c:if test="${not empty epChannelDivMappingsEdit.map.breadcrumbs}">
        <bean:size id="len" name="epChannelDivMappingsEdit" property="breadcrumbs"/>
        <c:forEach var="channel" items="${epChannelDivMappingsEdit.map.breadcrumbs}" varStatus="index">
          <c:url var="browseUrl" value="/ep/channelBrowse.do">
            <c:param name="channelId" value="${channel.map.id}"/>
          </c:url>
          <c:if test="${index.count > 1}"><bean-el:message key="ep.breadcrumb.delimiter" bundle="ep.messages"/>&nbsp;</c:if><c:if test="${index.count != len}"><!--a href="<c:out value="${browseUrl}"/>"--></c:if><c:out value="${channel.map.name}"/><c:if test="${index.count != len}"><!--/a--></c:if>
        </c:forEach>
      </c:if>
      </div>
<c:if test="${epChannelDivMappingsEdit.map.id != 0}">
	  <!-- Page sub-title -->
	  <div class="pageSubtitle">
        <span class="left"><bean-el:message key="ep.channel.div.mappings" bundle="ep.messages"/></span>
        <span class="right">
        </span>
      </div>

	  <!-- View form -->
	  <form name="epChannelDivMappingsEditForm" method="post">
      <html-el:hidden property="channelId" value="${epChannelDivMappingsEdit.map.id}"/>
      <input type="hidden" name="programId"/>
      <input type="hidden" name="ctx.f" value="<c:out value="${requestScope['ctx.f']}"/>"/>

	  <!-- Determine the page is for creating new or editing existing ones -->
	  <c:choose>
            <c:when test="${epChannelDivMappingsEdit.map.createNew}">
              <input type="hidden" name="createNew" value="1"/>
			</c:when>
            <c:otherwise>
              <input type="hidden" name="createNew" value="0"/>
            </c:otherwise>
      </c:choose>

      <!-- DIV mapping Table -->
      <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="Search result table" width="100%">
        <tr class="listHeader">
          <th id="DivName" align="left" width="20%"><bean-el:message key="ep.channel.div.mappings.name" bundle="ep.messages"/></th>
          <th id="ProgramName" align="left" width="40%"><bean-el:message key="ep.channel.div.mappings.program" bundle="ep.messages"/></th>
          <th id="CandidatePrograms" align="left" width="40%"><bean-el:message key="ep.channel.div.mappings.candidate" bundle="ep.messages"/></th>
		</tr>

<c:forEach var="item" items="${epChannelDivMappingsEdit.map.divMappings}" varStatus="index">
		<!-- Row color -->
        <c:choose>
            <c:when test="${(index.count + 1) % 2 == 0}">
              <tr class="listEven">
            </c:when> 
            <c:otherwise>
              <tr class="listOdd">
            </c:otherwise>
        </c:choose>

		<!-- Div name column -->
          <td headers="DivName">
        <c:choose>
            <c:when test="${epChannelDivMappingsEdit.map.createNew}">
			  <input type="text" name="divName" size="20" value=""/>
			</c:when>
            <c:otherwise>
		      <input type="text" name="divNewName" value="<c:out value="${item.map.divName}"/>" />
			  <input type="hidden" name="divName" value="<c:out value="${item.map.divName}"/>"/>
            </c:otherwise>
        </c:choose>
		  </td>

		<!-- Program name column -->
          <td headers="ProgramName"> 
        <c:choose>
            <c:when test="${epChannelDivMappingsEdit.map.createNew}">
		  <div id="programNameText" value=""></div>
			<input type="hidden" name="programName" value=""/>
			</c:when>
            <c:otherwise>
		  <div id="programNameText<c:out value="${item.map.divName}"/>"><c:out value="${item.map.programName}"/></div>
			<input type="hidden" name="programName<c:out value="${item.map.divName}"/>" value="<c:out value="${item.map.programName}"/>"/>
            </c:otherwise>
        </c:choose>
		  </td>

		<!-- Candidate program column -->
          <td headers="CandidatePrograms">  

		    <select name="candidatePrograms<c:out value="${item.map.divName}"/>" 
			  onChange="replaceProgram('<c:out value="${item.map.divName}"/>')">
		    <option><bean-el:message key="ep.channel.div.mappings.select.program" bundle="ep.messages"/></option>
			<option>---------------</option>
<c:forEach var="program" items="${epChannelDivMappingsEdit.map.childPrograms}" varStatus="idx">
	        <option><c:out value="${program.map.name}"/></option>
</c:forEach>
			<option>---------------</option>
			<option><bean-el:message key="ep.channel.div.mappings.more.selection" bundle="ep.messages"/></option>
		    </select>
          </td>
		</tr>
</c:forEach>

	  </table>


	<!-- Cancel and Save button -->
	 <div class="pageButtonWrap" align="right">
          <html-el:button styleClass="inputButton" property="cancelBtn" onclick="return doCancel(this.form);">
            <bean-el:message key="button.cancel" bundle="ep.messages"/>
          </html-el:button>
          &nbsp;
          <html-el:submit styleClass="inputButton" property="submitBtn" 
		   onclick="if (validateForm()) {return doDivMappingSave(this.form);} else {return false}">
            <bean-el:message key="button.save" bundle="ep.messages"/>
          </html-el:submit>
      </div>

</form>

</c:if>
<!-- End of DivMappings Section -->
    </td>
  </tr>
</table>
